home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / adg_7_8.zip / MDI.C < prev    next >
C/C++ Source or Header  |  1991-02-21  |  8KB  |  253 lines

  1. /****************************************************************************
  2. Module name: MDI.C
  3. Programmer : Jeffrey M. Richter & Elvira Peretsman.
  4. *****************************************************************************/
  5.  
  6. #include "..\nowindws.h"
  7. #undef NOCTLMGR
  8. #undef NOGDI
  9. #undef NOKERNEL
  10. #undef NOMB
  11. #undef NOMDI
  12. #undef NOMENUS
  13. #undef NOMSG
  14. #undef NOSYSMETRICS
  15. #undef NOUSER
  16. #undef NOWINMESSAGES
  17. #undef NOWINSTYLES
  18. #include <windows.h>
  19.  
  20. #include "mdi.h"
  21.  
  22.  
  23. // Application global variables.
  24. char   _szAppName[] = "MDI";  // The name of the application.
  25. HANDLE _hInstance = NULL;     // Data instance of the application.
  26. HANDLE _hAccelTable = NULL;   // Handle to active Accelerator table.
  27. HWND   _hWndMDIClient = NULL; // Handle to MDICLIENT window.
  28. HWND   _hDlgRibbon = NULL;    // Handle to Ribbon modeless dialog box.
  29.  
  30. /************************* Main Application Loop ****************************/
  31.  
  32.    // NEAR or FAR dependant on compilation model S or M respectively.
  33. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  34.                     LPSTR lpszCmdLine, int nCmdShow) {
  35.    MSG msg;
  36.    FARPROC fpProc;
  37.    HWND hWndFrame;
  38.  
  39.    if (hPrevInstance != NULL) {
  40.       // Only allow one instance of the application to run.
  41.       MessageBox(NULL, "MDI application is already running.", _szAppName,
  42.          MB_OK | MB_ICONINFORMATION);
  43.       return(0);
  44.    }
  45.  
  46.    _hInstance = hInstance;
  47.  
  48.    // Register the Frame window class.
  49.    if (!RegisterFrameWndClass()) return(0);
  50.  
  51.    // Register the MDI Child window classes.
  52.    if (!RegisterSheetWndClass()) return(0);
  53.    if (!RegisterChartWndClass()) return(0);
  54.  
  55.    // Create the Frame window.
  56.    hWndFrame = CreateWindow("Frame", _szAppName,
  57.       WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_MAXIMIZE | WS_VISIBLE |
  58.       WS_MAXIMIZEBOX | WS_MINIMIZEBOX,
  59.       CW_USEDEFAULT, nCmdShow, CW_USEDEFAULT, CW_USEDEFAULT,
  60.       NULL, NULL, _hInstance, NULL);
  61.    if (hWndFrame == NULL) return(0);
  62.  
  63.    // Create the Ribbon dialog box with Frame as owner.
  64.    fpProc = MakeProcInstance(RibbonDlgProc, _hInstance);
  65.    _hDlgRibbon = CreateDialog(_hInstance, "RIBBON", hWndFrame, fpProc);
  66.    if (_hDlgRibbon == NULL) {
  67.       FreeProcInstance(fpProc);
  68.       return(0);
  69.    }
  70.  
  71.    while (GetMessage(&msg, NULL, 0, 0)) {
  72.       if (!TranslateMDISysAccel(_hWndMDIClient, &msg)) {
  73.          if (_hAccelTable == NULL || !TranslateAccelerator(hWndFrame, _hAccelTable, &msg)) {
  74.             if (!IsDialogMessage(_hDlgRibbon, &msg)) {
  75.                TranslateMessage(&msg);
  76.                DispatchMessage(&msg);
  77.             }
  78.          }
  79.       }
  80.    }
  81.  
  82.    FreeProcInstance(fpProc);
  83.    return(msg.wParam);
  84. }
  85.  
  86. // Function to make creating MDI Child as easy as creating any other
  87. // kind of window.
  88. HWND FAR PASCAL CreateMDIChild (LPSTR szClassName, LPSTR szWindowName,
  89.                DWORD dwStyle, short x, short y, short nWidth, short nHeight,
  90.                HWND hWndMDIClient, HANDLE hInstance, LONG lParam) {
  91.    MDICREATESTRUCT cs;
  92.    HWND hWndChild;
  93.  
  94.    cs.szClass = szClassName;
  95.    cs.szTitle = szWindowName;
  96.    cs.hOwner  = hInstance;
  97.    cs.x       = x;
  98.    cs.y       = y;
  99.    cs.cx      = nWidth;
  100.    cs.cy      = nHeight;
  101.    cs.style   = dwStyle;
  102.    cs.lParam   = lParam;
  103.    hWndChild = (HWND) SendMessage(hWndMDIClient, WM_MDICREATE,
  104.                            0, (LONG) (LPMDICREATESTRUCT) &cs);
  105.    return(hWndChild);
  106. }
  107.  
  108.  
  109. // Function to change the menu in the Frame window whenever a 
  110. // new MDI Child becomes active.
  111. void FAR PASCAL ChangeMDIMenu (HWND hWndFrame, HWND hWndClient,
  112.          HMENU hMenuNew, WORD wMenuID) {
  113.    WORD wCount;
  114.    HMENU hSubMenu = 0;
  115.  
  116.    // Get number of top-level menu items in the menu used by the window
  117.    // being activated.
  118.    wCount = GetMenuItemCount(hMenuNew);
  119.  
  120.    // Locate the POPUP menu that contains the menu option with the 
  121.    // 'wMenuID' identifier in it.  This must be an identifier for an option
  122.    // in the new menu's "Window" popup menu.
  123.    while (wCount) {
  124.       hSubMenu = GetSubMenu(hMenuNew, wCount - 1);
  125.       if ((int) GetMenuState(hSubMenu, wMenuID, MF_BYCOMMAND) != -1)
  126.          break;
  127.       wCount--;
  128.    }
  129.  
  130.    // Tell the MDICLIENT window to setup the new menu.
  131.    SendMessage(hWndClient, WM_MDISETMENU, 0, MAKELONG(hMenuNew, hSubMenu));
  132.  
  133.    DrawMenuBar(hWndFrame);
  134. }
  135.  
  136.  
  137. // ***************************************************************************
  138. // This function processes all messages sent to the About dialog box.
  139.  
  140. BOOL FAR PASCAL AboutProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam) {
  141.    char szBuffer[100];
  142.    BOOL fProcessed = TRUE;
  143.  
  144.    switch (wMsg) {
  145.  
  146.       case WM_INITDIALOG:
  147.          // Set version static window to have date and time of compilation.
  148.          wsprintf(szBuffer, "%s at %s", (LPSTR) __DATE__, (LPSTR) __TIME__);
  149.          SetWindowText(GetDlgItem(hDlg, ID_VERSION), szBuffer);
  150.          break;
  151.  
  152.       case WM_COMMAND:
  153.          switch (wParam) {
  154.             case IDOK: case IDCANCEL:
  155.                if (HIWORD(lParam) == BN_CLICKED)
  156.                   EndDialog(hDlg, wParam);
  157.                break;
  158.  
  159.             default:
  160.                break;
  161.          }
  162.          break;
  163.  
  164.       default:
  165.          fProcessed = FALSE; break;
  166.    }
  167.    return(fProcessed);
  168. }
  169.  
  170.  
  171. BOOL FAR PASCAL RibbonDlgProc (HWND hDlg, WORD wMsg, WORD wParam, DWORD dwParam) {
  172.    BOOL fProcessed = TRUE;
  173.    HPEN hPen;
  174.    RECT rc;
  175.    PAINTSTRUCT ps;
  176.    HWND hCtl;
  177.    int i;
  178.    char szBuf[25];
  179.  
  180.    switch (wMsg) {
  181.  
  182.       case WM_INITDIALOG:
  183.          // Add strings to the font combobox.
  184.          hCtl = GetDlgItem(hDlg, ID_FONT);
  185.          i = IDS_FONT;
  186.          while (LoadString(_hInstance, i++, szBuf, sizeof(szBuf)) != 0)
  187.             SendMessage(hCtl, CB_ADDSTRING, 0, (LONG) (LPSTR) szBuf);
  188.          SendMessage(hCtl, CB_SETCURSEL, 0, 0);
  189.  
  190.          // Add strings to the fontsize combobox.
  191.          hCtl = GetDlgItem(hDlg, ID_SIZE);
  192.          i = IDS_SIZE;
  193.          while (LoadString(_hInstance, i++, szBuf, sizeof(szBuf)) != 0)
  194.             SendMessage(hCtl, CB_ADDSTRING, 0, (LONG) (LPSTR) szBuf);
  195.          SendMessage(hCtl, CB_SETCURSEL, 0, 0);
  196.          break;
  197.  
  198.       case WM_ENABLE:
  199.          // Make all child windows have the same status as the dialog box.
  200.          hCtl = GetWindow(hDlg, GW_CHILD);
  201.          while (hCtl != NULL) {
  202.             EnableWindow(hCtl, wParam);
  203.             hCtl = GetWindow(hCtl, GW_HWNDNEXT);
  204.          }
  205.          break;
  206.  
  207.       case WM_PAINT:
  208.          // Paint a horizontal dividing line between the Ribbon and the
  209.          // MDICLIENT window.
  210.          BeginPaint(hDlg, &ps);
  211.          hPen = CreatePen(PS_SOLID, GetSystemMetrics(SM_CYBORDER),
  212.             RGB(0, 0, 0));
  213.          SelectObject(ps.hdc, hPen);
  214.          GetClientRect(hDlg, &rc);
  215.          MoveTo(ps.hdc, 0, rc.bottom - GetSystemMetrics(SM_CYBORDER));
  216.          LineTo(ps.hdc, rc.right, rc.bottom - GetSystemMetrics(SM_CYBORDER));
  217.          EndPaint(hDlg, &ps);
  218.          DeleteObject(hPen);
  219.          break;
  220.  
  221.  
  222.       case WM_COMMAND:
  223.          // Make sure that focus is given back to the Frame after an 
  224.          // option is chosen by the user.
  225.  
  226.          switch (wParam) {
  227.             case ID_FONT:
  228.             case ID_SIZE:
  229.                if (HIWORD(dwParam) != CBN_SELCHANGE) break;
  230.                SetFocus(GetParent(hDlg));
  231.                break;
  232.  
  233.             case ID_BOLD:
  234.             case ID_ITALIC:
  235.             case ID_UNDERLINE:
  236.                if (HIWORD(dwParam) != BN_CLICKED) break;
  237.                SetFocus(GetParent(hDlg));
  238.                break;
  239.  
  240.             case IDOK:
  241.             case IDCANCEL:
  242.                SetFocus(GetParent(hDlg));
  243.                break;
  244.          }
  245.          break;
  246.  
  247.       default:
  248.          fProcessed = FALSE;
  249.          break;
  250.    }
  251.    return(fProcessed);
  252. }
  253.